package monitor

import (
	//"hxy/repo/cdelivery"
	"hxy/repo/cdelivery"
	"hxy/repo/dag"
	"hxy/repo/model"
	"log"
)

func ManualTraiggerCI(Repo model.GitModel) {

	rp, err := Repo.OpenRepo()
	if err != nil {
		log.Printf("Git Hash Error:%s", err)
		return
	}
	_hash, err := rp.Head()
	if err != nil {
		log.Printf("Git Hash Error:%s", err)
		return
	}
	var java_proj = dag.BuildJavaTask(Repo.Path, int(Repo.ID), _hash.Hash().String())
	java_proj.Run()
	// 触发cd
}

var TesttriggerByManager = triggerByManager

func triggerByManager(gm *GitManager) {

	rp, err := gm.Repo.OpenRepo()
	if err != nil {
		log.Printf("Git Hash Error:%s", err)
		return
	}
	_hash, err := rp.Head()
	if err != nil {
		log.Printf("Git Hash Error:%s", err)
		return
	}

	dagtask, err := dag.GetaDagtask(int(gm.Repo.ID))
	dagtask.GitHash = _hash.Hash().String()
	dagtask.Dir = gm.Repo.Path
	java_proj := dag.BuildJavaTask(gm.Repo.Path, int(gm.Repo.ID), _hash.Hash().String())

	if err != nil {
		// can't find a dag  task by given git id , run 默认 java run
		err = java_proj.Run()
		if err != nil {
			return
		} else {
			//触发Docker build & push
			cdelivery.DockerBuild(java_proj.Task)
		}

	} else {
		// if found a Dag task struct ,just run the dag ``
		genyaml, err2 := dagtask.GenYamlfromInterface()
		if err2 != nil {
			err = java_proj.Run()
		} else {
			dag.NewDagGraph(genyaml)
			dag.RunDAGTask(dagtask.IDName)
		}
	}

}
